package com.quickdemo.aspect;


import com.quickdemo.annotation.AutoFill;
import com.quickdemo.constant.AutoFillConstant;
import com.quickdemo.constant.OperationType;
import com.quickdemo.utils.IdGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * @author ckj
 */
@Slf4j
@Component
@Aspect
@RequiredArgsConstructor
public class AutoFillAspect {
    @Pointcut("execution(* com.quickdemo.mappers.*.*(..)) && @annotation(com.quickdemo.annotation.AutoFill)")
    public void autoFillPointCut(){}


    private final IdGenerator idGenerator;

    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint){

        //获取当前被拦截的方法上的数据库操作类型
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //获取方法AutoFill里的内容
        AutoFill autoFill = methodSignature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value();

        //获取被拦截的方法的传入的参数
        Object[] args = joinPoint.getArgs();
        if(args == null || args.length == 0){
            return;
        }

        Object entity = args[0];

        //准备数据
        LocalDateTime now = LocalDateTime.now();
        Long currentId = idGenerator.snowflakeId();

        if(operationType == OperationType.INSERT){
            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME,LocalDateTime.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                Method setCreatedBy = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER,Long.class);
                Method setUpdatedBy = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);

                setCreateTime.invoke(entity,now);
                setUpdateTime.invoke(entity,now);
                setCreatedBy.invoke(entity,currentId);
                setUpdatedBy.invoke(entity,currentId);
            } catch (Exception e) {
                log.error("添加用户操作失败");
            }

        }else if(operationType == OperationType.UPDATE){
            try {
                //通过反射来获取操作的方法   实体获取类然后获取方法 传入的参数（ 方法名称  ，该方法传入的参数）
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                Method setUpdatedBy = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);

                //通过反射为对象赋值
                setUpdateTime.invoke(entity,now);
                setUpdatedBy.invoke(entity,currentId);
            } catch (Exception e) {
                log.error("更新用户操作失败");
            }

        }
    }
}
